<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/24
 * Time: 9:26
 */

namespace app\api\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\UploadWork;
use app\api\model\Works as WorksModel;
use app\api\model\User as UserModel;
/**
 * @title 作品管理
 * @group app
 */
class Works extends Base
{
    /**
     * @title 上传作品
     * @author 开发者
     * @url /api/works/uploadWork
     * @method POST/GET
     * @param name:img require:1 default:null type:varchar desc:作品地址
     * @param name:subject_id require:1 default:null type:int desc:科目id
     * @param name:user_id require:1 default:null type:int desc:用户id
     */
    public function uploadWork(){
        (new UploadWork())->goCheck();
         $data = [
             'img' => input('img'),
             'subject_id' => input('subject_id'),
             'user_id' => input('user_id'),
         ];
         $user_info = UserModel::getUserInfoById($data['user_id']);
         if(!$user_info['school']||!$user_info['username']){
             $this->apiReturn(303,'请先认证！');
         }
         //判断用户是否已经上传
         $info = WorksModel::getInfoBySubjectUser($data['subject_id'],$data['user_id']);
         if(!$info){
             if(WorksModel::create($data)){
                 $this->apiReturn(200,'上传成功！');
             }else{
                 $this->apiReturn(303,'上传失败！');
             }
         }else{
             $info->img = $data['img'];
             $res= $info->save();
             if($res){
                 $this->apiReturn(200,'上传成功！');
             }else{
                 $this->apiReturn(303,'上传失败！');
             }
         }

    }

    /**
     *  @title 通过科目id展示作品
     * @author 开发者
     * @url /api/works/getInfoBySubject
     * @method POST/GET
     * @description 测试的科目id为1
     * @param name:id require:1 default:null type:int desc:科目id
     * @param name:page require:0 default:1 type:int desc:当前页数,默认为1
     * @param name:page_size require:0 default:20 type:int desc:每页显示记录数,默认20
     * @return id:作品id
     * @return img:作品地址
     * @return user_id:用户id
     */
    public function getInfoBySubject(){
        $subject_id = input('id');
        (new IDMustBePositiveInt())->goCheck();
        //让前端设置数据分几页显示
        $page = input('page', 1, 'intval');
        //让前端设置数据一页显示几条数据
        $page_size = input('page_size', 20, 'intval');
        $result = WorksModel::getInfoBySubject($subject_id,$page,$page_size);
        $this->apiReturn(200,'获取成功',$result);
    }
    /**
     *  @title 根据科目和用户id获取用户的作品
     * @author 开发者
     * @url /api/works/getUserWork
     * @method POST/GET
     * @description 测试的科目id和用户id为1
     * @param name:subject_id require:1 default:null type:int desc:科目id
     * @param name:user_id require:1 default:null type:int desc:用户id
     * @return id:作品id
     * @return img:作品地址
     * @return user_id:用户id
     * @return  create_time:创建时间
     */
    public function getUserWork()
    {
        //接受数据
        $data = I('subject_id|user_id');
        //循环遍历判断数据是否缺少
        foreach ($data as $k => $v){
            if (!$v) {
                $this->apiReturn(303, '数据缺少' . $k);
            }
        }
        $result = WorksModel::where($data)->find();
        if(!$result){
            $this->apiReturn(303,'未找到相关信息！');
        }else{
            $this->apiReturn(200,'获取成功',$result);
        }
    }
}